package first.group.ssh.writebuffer;

import first.group.ssh.util.ByteBufferUtil;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class ByteArrayContent extends WriteContent {
    private final ByteBufferContent content;
    
    public ByteArrayContent(String content) {
        this(content.getBytes(StandardCharsets.US_ASCII));
    }
    
    public ByteArrayContent(byte[] content) {
        this(content, 0, content.length, false);
    }
    
    public ByteArrayContent(byte[] content, int off, int len) {
        this(content, off, len, false);
    }
    
    public ByteArrayContent(byte[] content, int off, int len, boolean copy) {
        var buffer = ByteBuffer.wrap(content);
        buffer.position(off);
        buffer.limit(off + len);
        if (copy) {
            this.content = new ByteBufferContent(ByteBufferUtil.copyCompatedByteBuffer(buffer));
        } else {
            this.content = new ByteBufferContent(buffer);
        }
    }
    
    @Override
    public int write(SocketChannel channel) throws IOException {
        return content.write(channel);
    }

    @Override
    public int write(ByteBuffer buffer) {
        return content.write(buffer);
    }

    @Override
    public boolean hasRemaining() {
        return content.hasRemaining();
    }

    @Override
    public int getRemaining() {
        return content.getRemaining();
    }
}
